﻿using AnesSystem.Models;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using AnesSystem.Controls;
using AnesSystem.ViewModels.Configuration.DataGrid;
using System.IO;
using AnesSystem.Util;
using System.Windows;

namespace AnesSystem.ViewModels
{
    public class XuanZeShouShuCtrlViewModel : BaseViewModel<XuanZeShouShuCtrlViewModel>
    {
        #region Variables 

        private List<SelPatientLinkModel> _oprationList;

        private ObservableCollection<SelPatientLinkModel> _displayOprList;

        private ObservableCollection<ConfigOperationRoomModel> _displayConfigOperationRoomModelList;

        private int _displayCount;

        private ConfigOperationRoomModel _ssj = new ConfigOperationRoomModel();

        private string _sszt = "";

        private string findValue;

        private Boolean _isCheck = false;

        private SelPatientLinkModel selectedValue;

        private bool selectTomDay = false;

        private Visibility _deleteIsVis;

        private List<string> _delNotShowHospitalList;


        #endregion

        #region Constructor

        public XuanZeShouShuCtrlViewModel()
        {
            _displayOprList = new ObservableCollection<SelPatientLinkModel>();
            _oprationList = new List<SelPatientLinkModel>();
            InitDelShowList();
            DisplayOprList.CollectionChanged += DisplayOprList_CollectionChanged;
            DeleteIsVis = Visibility.Visible;
            foreach (var item in _delNotShowHospitalList)
            {
                if (DataManager.Instance.CurrentHospital.HospitalCode.Equals(item))
                {
                    DeleteIsVis = Visibility.Collapsed;
                    break;
                }
            }
            InitailOprations();
        }

        private void InitDelShowList()
        {
            _delNotShowHospitalList = new List<string>();
            _delNotShowHospitalList.Add(HospitalConfig.HOSPITAL_CODE_CQ_NTZYY);
            _delNotShowHospitalList.Add(HospitalConfig.HOSPITAL_CODE_CQ_FYBJY);
            _delNotShowHospitalList.Add(HospitalConfig.HOSPITAL_CODE_SC_SCDC);
            _delNotShowHospitalList.Add(HospitalConfig.HOSPITAL_CODE_CQ_TLRMYY);
            _delNotShowHospitalList.Add(HospitalConfig.HOSPITAL_CODE_SC_CD_TYFSYY);
        }

        #endregion

        #region Properties

        public List<SelPatientLinkModel> OprationList
        {
            get
            {
                return _oprationList;
            }

            set
            {
                _oprationList = value;
            }
        }

        public int DisplayCount
        {
            get
            {
                return _displayOprList.Count();
            }

            set
            {
                _displayCount = value;
                RaisePropertyChanged("DisplayCount");
            }
        }

        public ObservableCollection<SelPatientLinkModel> DisplayOprList
        {
            get
            {
                return _displayOprList;
            }

            set
            {
                _displayOprList = value;
            }
        }

        public ConfigOperationRoomModel Ssj
        {
            get
            {
                return _ssj;
            }

            set
            {
                _ssj = value;
                RaisePropertyChanged("Ssj");
            }
        }

        public string Sszt
        {
            get
            {
                return _sszt;
            }

            set
            {
                _sszt = value;
                RaisePropertyChanged("Sszt");
            }
        }

        public string FindValue
        {
            get
            {
                if (findValue == null)
                    return "";
                return findValue;
            }

            set
            {
                findValue = value;
                Serch();
                RaisePropertyChanged("FindValue");
            }
        }

        public IEnumerable<ConfigOperationRoomModel> DisplayConfigOperationRoomModelList
        {
            get
            {
                ObservableCollection<ConfigOperationRoomModel> list = new ObservableCollection<ConfigOperationRoomModel>();
                list.Add(new ConfigOperationRoomModel());
                return list.Union(DataManager.Instance.ConfigOperationRoomModelList);
            }
        }

        public SelPatientLinkModel SelectedValue
        {
            get
            {
                return selectedValue;
            }
            set
            {
                if (value == null) return;
                selectedValue = value;
                selectedValue.IsChecked = true;
                DataManager.Instance.CurrentPatientLink = value.PatientLink;
                //if (DataManager.Instance.CurrentPatientLink == null || (DataManager.Instance.CurrentPatientLink != null && !DataManager.Instance.CurrentPatientLink.Id.Equals(value.PatientLink.Id)))
                //{
                //    DataManager.Instance.CurrentPatientLink = value.PatientLink;
                //}

                RaisePropertyChanged("SelectedValue");
            }
        }

        public Visibility DeleteIsVis
        {
            get
            {
                return _deleteIsVis;
            }

            set
            {
                _deleteIsVis = value;
                RaisePropertyChanged("DeleteIsVis");
            }
        }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods


        private void DisplayOprList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged("DisplayCount");
        }

        private void Serch()
        {
            try
            {
                DisplayOprList.Clear();
                foreach (var item in OprationList)
                {
                    string str = item.PatientLink.Patient.Brxm
                                 +
                                 //item.PatientLink.Patient.Xb 
                                 //+ 
                                 //item.PatientLink.Patient.Nl 
                                 //+ 
                                 item.PatientLink.Patient.Ksmc
                                 +
                                 item.PatientLink.Patient.Zych
                                 +
                                 item.PatientLink.Patient.Zyfh
                                 + "";
                    //+ 
                    //item.PatientLink.Application.Nsssmc
                    //+ 
                    //item.PatientLink.Application.Sqzd1 
                    //+ 
                    //item.PatientLink.Schedule.Mzysbh1Name 
                    //+ 
                    //item.PatientLink.Schedule.Ssysbh
                    ;
                    if (Ssj == null || (Ssj != null && string.IsNullOrEmpty(Ssj.Name) || Ssj.Name.Equals(item.PatientLink.Schedule.Pbssjh)) &&
                        (string.IsNullOrEmpty(Sszt) || Sszt.Equals(item.PatientLink.Application.Detailstatus)) &&
                        (string.IsNullOrEmpty(findValue) || str.IsContain(findValue)))
                    {
                        DisplayOprList.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        private void InitailOprations()
        {
            try
            {
                _oprationList.Clear();
                List<PatientLinkModel> list = PatientLinkService.Instance.GetSelTableOpers();
                var tempDic = list.GroupBy(O => O.Schedule.Pbssjh).OrderBy(O => int.Parse(O.Key));
                for (int i = 0; i < tempDic.Count(); i++)
                {
                    List<SelPatientLinkModel> lac = new List<SelPatientLinkModel>();
                    var group = tempDic.ElementAt(i);
                    foreach (var item in group)
                    {
                        SelPatientLinkModel model = new SelPatientLinkModel();
                        model.PatientLink = item;
                        if (DataManager.Instance.CurrentPatientLink != null && DataManager.Instance.CurrentPatientLink.Id.Equals(item.Id))
                        {
                            SelectedValue = model;
                        }
                        model.PatientLink = item;
                        lac.Add(model);
                    }
                    OprationList.AddRange(lac.OrderBy(O => O.PatientLink.Schedule.PbtsInt));
                }
                Serch();
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void InitailTomorrowOprations()
        {
            DisplayOprList.Clear();
            _oprationList.Clear();
            List<PatientLinkModel> list = PatientLinkService.Instance.GetTomorrowSelTableOpers();
            var tempDic = list.GroupBy(O => O.Schedule.Pbssjh).OrderBy(O => int.Parse(O.Key));
            for (int i = 0; i < tempDic.Count(); i++)
            {
                List<SelPatientLinkModel> lac = new List<SelPatientLinkModel>();
                var group = tempDic.ElementAt(i);
                foreach (var item in group)
                {
                    SelPatientLinkModel model = new SelPatientLinkModel();
                    model.PatientLink = item;
                    if (DataManager.Instance.CurrentPatientLink != null && DataManager.Instance.CurrentPatientLink.Id.Equals(item.Id))
                    {
                        SelectedValue = model;
                    }
                    model.PatientLink = item;
                    lac.Add(model);
                }
                OprationList.AddRange(lac.OrderBy(O => O.PatientLink.Schedule.PbtsInt));
                Serch();
            }
        }

        private void InitailAllOprations()
        {
            DisplayOprList.Clear();
            _oprationList.Clear();
            List<PatientLinkModel> list = PatientLinkService.Instance.GetAllSelTableOpers();
            var tempDic = list.GroupBy(O => O.Schedule.Pbssjh).OrderBy(O => int.Parse(O.Key));
            for (int i = 0; i < tempDic.Count(); i++)
            {
                List<SelPatientLinkModel> lac = new List<SelPatientLinkModel>();
                var group = tempDic.ElementAt(i);
                foreach (var item in group)
                {
                    SelPatientLinkModel model = new SelPatientLinkModel();
                    model.PatientLink = item;
                    if (DataManager.Instance.CurrentPatientLink != null && DataManager.Instance.CurrentPatientLink.Id.Equals(item.Id))
                    {
                        SelectedValue = model;
                    }
                    model.PatientLink = item;
                    lac.Add(model);
                }
                OprationList.AddRange(lac.OrderBy(O => O.PatientLink.Schedule.PbtsInt));
                Serch();
            }
        }
        #endregion

        #region Public/Protected Methods

        public override void UdpRef(PatientLinkModel pm = null)
        {
            foreach (var item in OprationList)
            {
                if ((_oprationList.Where(O => O.PatientLink.Id.Equals(pm.Id)).Count() == 0 && pm.Schedule.Jhsssj != new DateTime() && pm.Schedule.Jhsssj.Date.Equals(DateTime.Now.Date)) ||
                    (pm.Id.Equals(item.PatientLink.Id) &&
                       ((pm.Patient.Status != null && !pm.Patient.Status.Equals(item.PatientLink.Patient.Status)) ||
                         (pm.Schedule.Pbssjh != null && !pm.Schedule.Pbssjh.Equals(item.PatientLink.Schedule.Pbssjh)) ||
                         pm.Schedule.PbtsInt != item.PatientLink.Schedule.PbtsInt ||
                         (pm.Application.Detailstatus != null &&
                         !pm.Application.Detailstatus.Equals(item.PatientLink.Application.Detailstatus)))))
                {
                    if (selectTomDay)
                    {
                        InitailTomorrowOprations();
                    }
                    else
                    {
                        InitailOprations();
                    }
                    break;
                }
            }
        }

        public void RefView()
        {
            if (selectTomDay)
            {
                InitailTomorrowOprations();
            }
            else
            {
                InitailOprations();
            }
        }

        public void Excute(string comand)
        {
            switch (comand)
            {
                //case "cs":
                //    Test();
                //    break;
                case "sx":
                    RefList();
                    break;
                case "all":
                    AllOperList();
                    break;
                case "mt":
                    MingTList();
                    break;
                case "jt":
                    ToDayList();
                    break;
                case "bssj":
                    BssjList();
                    break;
                case "qhssj":
                    Serch();
                    break;
                case "sszt":
                    Serch();
                    break;
                case "likeFind":
                    Serch();
                    break;
                case "dataGridMouseDouble":
                    DataGridMouseDouble();
                    break;
            }
        }
        //dataGrid双击跳转麻醉单
        public void DataGridMouseDouble()
        {
            if (DataManager.Instance.CurrentPatientLink == null)
            {
                MessageBoxCommon.ShowDialog("请选择一个病人！", MessageBoxCommon.OK);
                return;
            }
            if (DataManager.Instance.CurrentPatientLink.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_IN_PCU))
            {
                MainPageCommonViewModel.Instance.NavigateTo("PacuRecordCtrl", false);
            }
            else
            {
                MainPageCommonViewModel.Instance.NavigateTo("AnesRecordCtrl", false);
            }
        }


        public void RefList()
        {
            _sszt = "";
            OprationList.Clear();
            _displayOprList.Clear();
            if (selectTomDay)
            {
                InitailTomorrowOprations();
            }
            else
            {
                InitailOprations();
            }
        }

        public void BssjList()
        {
            Ssj = DataManager.Instance.CurRoom;
            DisplayOprList.Clear();
            Serch();

        }
        public void AllOperList()
        {
            InitailAllOprations();
            selectTomDay = false;
        }
        public void MingTList()
        {
            InitailTomorrowOprations();
            selectTomDay = true;
        }
        public void ToDayList()
        {
            //DisplayOprList.Clear();
            //Serch();
            //for (int i = DisplayOprList.Count - 1; i >= 0; i--)
            //{
            //    var item = DisplayOprList[i];
            //    if (!(item.PatientLink.Schedule.Jhsssj > new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
            //         && item.PatientLink.Schedule.Jhsssj < new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1)))
            //    {
            //        DisplayOprList.RemoveAt(i);
            //    }
            //}
            InitailOprations();
            selectTomDay = false;
        }
        public void UpdateStatusForRemove(SelPatientLinkModel patientLinkModel)
        {
            _oprationList.Remove(patientLinkModel);
            DisplayOprList.Remove(patientLinkModel);
            patientLinkModel.PatientLink.Patient.Status = Constants.FLAG_IN_VALID;
            PatientLinkService.Instance.Update(patientLinkModel.PatientLink);
        }


        public void Export()
        {
            bool a1 = false;
            try
            {
                string fileName = DateTime.Now.ToString("yyyy-MM-dd");
                //= "e://" + "手术记录" + fileName + ".txt";
                String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\手术记录" + fileName + ".txt";
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                string part1 = "";
                string part2 = "";
                string mzssyd = "";
                string sperateLine = "————————————————————————————————————————\r\n";
                foreach (SelPatientLinkModel o in DisplayOprList)
                {
                    List<string> jwss = DataManager.Instance.GetXMLTableValue(o.PatientLink, "NanTongShuQianFangShi",
                    new string[] { "gxy", "gxb", "tnb", "mxzsxfy", "xc", "nxs", "ncx"
                , "dx", "sss","mzs", "wss", "gms", "sxs", "xys", "yjy", "jzs" ,"tsqk"});
                    string jwsStr = "";
                    //既往史
                    if (jwss.Count == 17)
                    {
                        bool.TryParse(jwss[0], out a1);
                        jwsStr += (!a1 ? "" : "高血压  ");
                        bool.TryParse(jwss[1], out a1);
                        jwsStr += (!a1 ? "" : "冠心病  ");
                        bool.TryParse(jwss[2], out a1);
                        jwsStr += (!a1 ? "" : "糖尿病  ");
                        bool.TryParse(jwss[3], out a1);
                        jwsStr += (!a1 ? "" : "慢性阻塞性肺炎  ");
                        bool.TryParse(jwss[4], out a1);
                        jwsStr += (!a1 ? "" : "哮喘  ");
                        bool.TryParse(jwss[5], out a1);
                        jwsStr += (!a1 ? "" : "脑血塞  ");
                        bool.TryParse(jwss[6], out a1);
                        jwsStr += (!a1 ? "" : "脑出血  ");
                        bool.TryParse(jwss[7], out a1);
                        jwsStr += (!a1 ? "" : "癫痫  ");
                        bool.TryParse(jwss[8], out a1);
                        jwsStr += (!a1 ? "" : "手术史  ");
                        bool.TryParse(jwss[9], out a1);
                        jwsStr += (!a1 ? "" : "麻醉史  ");
                        bool.TryParse(jwss[10], out a1);
                        jwsStr += (!a1 ? "" : "外伤史  ");
                        bool.TryParse(jwss[11], out a1);
                        jwsStr += (!a1 ? "" : "过敏史  ");
                        bool.TryParse(jwss[12], out a1);
                        jwsStr += (!a1 ? "" : "输血史  ");
                        bool.TryParse(jwss[13], out a1);
                        jwsStr += (!a1 ? "" : "吸烟史  ");
                        bool.TryParse(jwss[14], out a1);
                        jwsStr += (!a1 ? "" : "已戒烟  ");
                        bool.TryParse(jwss[15], out a1);
                        jwsStr += (!a1 ? "" : "家族史  ");
                        jwsStr += (null == jwss[16] ? "" : "\r\n特殊情况:" + jwss[16] + "\r\n");
                    }
                    List<string> jys = new List<string>();
                    jys = DataManager.Instance.GetXMLTableValue(o.PatientLink, "NanTongShuQianFangShi",
                        new string[] {"rbc","hb","hct","plt","wbc","ncg", "djz",  "ggn","sgn","xuetang" ,"thdb"
                        ,"nxgn" ,"ecg" ,"xqfx" ,"fgn","xp","jg", "yg", "bg" ,"khiv","md"
                        ,"qitajy", "chsxdt","asa1","asa2","asa3","asa4","asa5","asa6","asae","nnis1"
                        ,"nnis2","nnis3","nnis4","xgn1","xgn2","xgn3","xgn4","mzssyd1"});

                    //术前检查
                    string jyStr = "";
                    if (jys.Count() == 39)
                    {
                        jyStr += (null == o.PatientLink.Patient.Xx ? "         血型:" : "         血型:" + o.PatientLink.Patient.Xx + "\r\n");//血型
                        jyStr += (null == jys[0] ? "         血常规:" : "         血常规:RBC: " + jys[0] + "x10^12/L; ");//RBC
                        jyStr += (null == jys[1] ? "HB: " : "HB: " + jys[1] + "g/L; ");//HB
                        jyStr += (null == jys[2] ? "HCT: " : "HCT: " + jys[2] + "%; ");//Hct
                        jyStr += (null == jys[3] ? "PLT: " : "PLT: " + jys[3] + "x10^9/L; ");//plt
                        jyStr += (null == jys[4] ? "WBC: " : "WBC: " + jys[4] + "x10^9/L;\r\n");//WBC
                        jyStr += (null == jys[5] ? "         尿常规:" : "         尿常规:" + jys[5] + "\r\n");//尿常规
                        jyStr += (null == jys[6] ? "         电解质:" : "         电解质:" + jys[6] + "\r\n");//电解质
                        jyStr += (null == jys[7] ? "         肝功能:" : "         肝功能:" + jys[7] + "\r\n");//肝功能
                        jyStr += (null == jys[8] ? "         肾功能:" : "         肾功能:" + jys[8] + "\r\n");//肾功能
                        jyStr += (null == jys[9] ? "         血糖:" : "         血糖:" + jys[9] + "\r\n");//
                        jyStr += (null == jys[10] ? "         糖化血红蛋白:" : "         糖化血红蛋白:" + jys[10] + "\r\n");//糖化血红蛋白
                        jyStr += (null == jys[11] ? "         凝血功能:" : "         凝血功能:" + jys[11] + "\r\n");//凝血功能
                        jyStr += (null == jys[12] ? "         ECG:" : "         ECG:" + jys[12] + "\r\n");//ECG
                        jyStr += (null == jys[13] ? "         血气分析:" : "         血气分析:" + jys[13] + "\r\n");//血气分析
                        jyStr += (null == jys[14] ? "         肺功能:" : "         肺功能:" + jys[14] + "\r\n");//肺功能
                        jyStr += (null == jys[15] ? "         胸片:" : "         胸片:" + jys[15] + "\r\n");//胸片

                        bool.TryParse(jys[16], out a1);
                        string ss = !a1 ? "" : "甲肝 ";
                        string aa = "         传染病:" + ss;
                        bool.TryParse(jys[17], out a1);
                        aa += !a1 ? "" : "乙肝 ";
                        //aa = aa + ss;
                        bool.TryParse(jys[18], out a1);
                        aa += !a1 ? "" : "丙肝 ";
                        // aa = aa + ss;
                        bool.TryParse(jys[19], out a1);
                        aa += !a1 ? "" : "抗HIV ";
                        // aa = aa + ss;
                        bool.TryParse(jys[20], out a1);
                        aa += !a1 ? "" : "梅毒 ";
                        //aa = aa + ss;
                        if (aa != null && !("").Equals(aa))
                        {
                            jyStr += (aa + "\r\n");
                        }
                        jyStr += (null == jys[21] ? "" : "         其他检验:" + jys[21] + "\r\n");//其他检验
                        jyStr += (null == jys[22] ? "" : "         超声心动图:" + jys[22] + "\r\n");//超声心动图

                        string asas = "         ASA分级:";
                        bool.TryParse(jys[23], out a1);
                        string asa = !a1 ? "" : "Ⅰ级 ";
                        bool.TryParse(jys[24], out a1);
                        asa += !a1 ? "" : "Ⅱ级 ";
                        bool.TryParse(jys[25], out a1);
                        asa += !a1 ? "" : "Ⅲ级 ";
                        bool.TryParse(jys[26], out a1);
                        asa += !a1 ? "" : "Ⅳ级 ";
                        bool.TryParse(jys[27], out a1);
                        asa += !a1 ? "" : "Ⅴ级 ";
                        bool.TryParse(jys[28], out a1);
                        asa += !a1 ? "" : "Ⅵ级 ";
                        bool.TryParse(jys[29], out a1);
                        asa += !a1 ? "" : "--E ";
                        if (aa != null)
                        {
                            jyStr += (asas + asa + "\r\n");
                        }

                        string nnis = "         NNIS分级:";
                        bool.TryParse(jys[30], out a1);
                        string nis = !a1 ? "" : "0级 ";
                        bool.TryParse(jys[31], out a1);
                        nis += !a1 ? "" : "1级 ";
                        bool.TryParse(jys[32], out a1);
                        nis += !a1 ? "" : "2级 ";
                        bool.TryParse(jys[33], out a1);
                        nis += !a1 ? "" : "3级 ";
                        if (nis != null)
                        {
                            jyStr += (nnis + nis + "\r\n");
                        }

                        string nyhas = "         心功能分级:";
                        bool.TryParse(jys[34], out a1);
                        string nyha = !a1 ? "" : "1级 ";
                        bool.TryParse(jys[35], out a1);
                        nyha += !a1 ? "" : "2级 ";
                        bool.TryParse(jys[36], out a1);
                        nyha += !a1 ? "" : "3级 ";
                        bool.TryParse(jys[37], out a1);
                        nyha += !a1 ? "" : "4级 ";
                        if (nyha != null)
                        {
                            jyStr += (nyhas + nyha + "\r\n");
                        }
                        mzssyd = jys[38] == null ? "" : jys[38];//麻醉实施要点
                    }
                    part1 += (null == o.PatientLink.Patient.Ksmc ? "" : o.PatientLink.Patient.Ksmc + "-");
                    part1 += (null == o.PatientLink.Patient.Zych ? "" : o.PatientLink.Patient.Zych + ",");
                    part1 += (null == o.PatientLink.Patient.Brxm ? "" : o.PatientLink.Patient.Brxm + ",");
                    part1 += (null == o.PatientLink.Patient.Xb ? "" : o.PatientLink.Patient.Xb + ",");
                    part1 += (null == o.PatientLink.Patient.Nl ? "" : o.PatientLink.Patient.Nl + "\r\n");
                    part1 += ("术前诊断:");
                    part1 += (o.PatientLink.Application.Sqzd1 + "\r\n");
                    part1 += ("拟施手术:");
                    part1 += (o.PatientLink.Application.Nsssmc + "\r\n");
                    part1 += ("既往史:");
                    part1 += (jwsStr);
                    part1 += ("手术史:\r\n");
                    part1 += ("特殊情况:/\r\n");
                    part1 += ("术前检查:\r\n" + jyStr);
                    List<string> pgs = new List<string>();
                    pgs = DataManager.Instance.GetXMLTableValue(o.PatientLink, "NanTongShuQianFangShi", new string[] { "pgc", "pgzd", "pglh", "pgyx" });
                    string pgStr = "";
                    if (pgs.Count == 4)
                    {
                        bool.TryParse(pgs[0], out a1);
                        string hdzk = !a1 ? "" : "差 ";
                        bool.TryParse(pgs[1], out a1);
                        hdzk += !a1 ? "" : "中等";
                        bool.TryParse(pgs[2], out a1);
                        hdzk += !a1 ? "" : "良好";
                        bool.TryParse(pgs[3], out a1);
                        hdzk += !a1 ? "" : "优秀";
                        if (hdzk != null)
                        {
                            pgStr += (hdzk);
                        }
                    }
                    List<string> qdpgs = new List<string>();
                    qdpgs = DataManager.Instance.GetXMLTableValue(o.PatientLink, "NanTongShuQianFangShi", new string[] { "qdwkn", "cgkn", "tqkn" });
                    string qdpgStr = "";
                    if (qdpgs.Count == 3)
                    {
                        bool.TryParse(qdpgs[0], out a1);
                        string qdpg = !a1 ? "" : "无困难 ";
                        bool.TryParse(qdpgs[1], out a1);
                        qdpg += !a1 ? "" : "插管困难 ";
                        bool.TryParse(qdpgs[2], out a1);
                        qdpg += !a1 ? "" : "通气困难 ";
                        if (qdpg != null)
                        {
                            qdpgStr += (qdpg);
                        }
                    }
                    List<string> mzfss = new List<string>();
                    mzfss = DataManager.Instance.GetXMLTableValue(o.PatientLink, "NanTongShuQianFangShi", new string[] { "qm", "zgmz", "sjzz" });
                    StringBuilder mzfsStr = new StringBuilder();
                    if (mzfss.Count == 3)
                    {
                        if (!string.IsNullOrEmpty(mzfss[0]) && bool.Parse(mzfss[0]))
                        {
                            if (!string.IsNullOrEmpty(mzfsStr.ToString())) mzfsStr.Append(" + ");
                            mzfsStr.Append("全麻");
                        }
                        if (!string.IsNullOrEmpty(mzfss[1]) && bool.Parse(mzfss[1]))
                        {
                            if (!string.IsNullOrEmpty(mzfsStr.ToString())) mzfsStr.Append(" + ");
                            mzfsStr.Append("椎管内麻醉");
                        }
                        if (!string.IsNullOrEmpty(mzfss[2]) && bool.Parse(mzfss[2]))
                        {
                            if (!string.IsNullOrEmpty(mzfsStr.ToString())) mzfsStr.Append(" + ");
                            mzfsStr.Append("神经阻滞");
                        }
                    }
                    part2 += "活动状况评估:" + pgStr + "\r\n";
                    part2 += "通气与插管风险评估:" + qdpgStr + "\r\n";
                    part2 += "围术期血栓栓塞性疾病风险:" + "" + "\r\n";
                    part2 += "围术期心脏风险:" + "" + "\r\n";
                    part2 += "麻醉方式:" + mzfsStr + "\r\n";
                    part2 += "麻醉计划:" + mzssyd + "\r\n";
                    part2 += "实施手术:" + o.PatientLink.AnesthesiaRecord.Ysss1 + "\r\n";
                    part2 += "麻醉经过:" + o.PatientLink.AnesthesiaRecord.Szzj + "\r\n";
                    part2 += "术后随访:\r\n";
                    string st3 = part1 + "\r\n" + part2 + "\r\n" + sperateLine;
                    //开始写入
                    sw.Write(st3);
                    //清空缓冲区
                    sw.Flush();
                    part2 = "";
                    part1 = "";
                    jyStr = "";
                    jwsStr = "";
                }
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception e)
            {
                Common.LogHelper.Error(this.GetType(), e);
            }
        }

        public void Test()
        {
            List<PatientLinkModel> operationList = PatientLinkService.Instance.FindAll().ToList();
            List<PatientVitalSignsModel> listVitalSigns = null;
            List<PatientDrugsModel> listDrugs = null;
            List<PatientEventsModel> listevents = null;
            foreach (var items in operationList)
            {
                listevents = PatientEventsService.Instance.GetPatientEvents8Id(items.Id.ToString());
                if (listevents==null|| listevents.Count==0) {
                    PatientLinkService.Instance.DeleteById(items.Id);
                    continue;
                }
                listVitalSigns = PatientVitalSignsServicie.Instance.GetByPatientID(items.Id.ToString());
                listDrugs = PatientDrugsService.Instance.GetPatientDrugs8Pid(items.Id.ToString());
                List<PatientVitalSignsModel> VitalSignsList = new List<PatientVitalSignsModel>();
                foreach (var item in listVitalSigns)
                {
                    //PatientDrugsModel pDrug = PacuSpecialDrugList.ToList().Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                    List<PatientVitalSignsModel> pvlist = VitalSignsList.ToList().FindAll(delegate (PatientVitalSignsModel p)
                    {
                        return p.Code.Equals(item.Code) && p.CaptureTime == item.CaptureTime;
                    });//返回所有匹配
                    if (pvlist == null || pvlist.Count == 0)
                    {
                        VitalSignsList.Add(item);
                    }
                    else
                    {
                        PatientVitalSignsServicie.Instance.DeleteById(item.Id);
                    }
                }
                List<PatientEventsModel> EventsList = new List<PatientEventsModel>();
                foreach (var item in listevents)
                {
                    //PatientDrugsModel pDrug = PacuSpecialDrugList.ToList().Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                    List<PatientEventsModel> pvlist = EventsList.ToList().FindAll(delegate (PatientEventsModel p)
                    {
                        return p.Code.Equals(item.Code) && p.BeginTime == item.BeginTime;
                    });//返回所有匹配
                    if (pvlist == null || pvlist.Count == 0)
                    {
                        EventsList.Add(item);
                    }
                    else
                    {
                        PatientEventsService.Instance.DeleteById(item.Id);
                    }
                }
                List<PatientDrugsModel> DrugsList = new List<PatientDrugsModel>();
                foreach (var item in listDrugs)
                {
                    List<PatientDrugsModel> pvlist = DrugsList.ToList().FindAll(delegate (PatientDrugsModel p)
                    {
                        return p.BeginTime.Equals(item.BeginTime) && p.Drugs.HashCode == item.Drugs.HashCode;
                    });//返回所有匹配
                    if (pvlist == null || pvlist.Count == 0)
                    {
                        DrugsList.Add(item);
                    }
                    else
                    {
                       PatientDrugsService.Instance.DeleteById(item.Id);
                    }
                }
            }
            MessageBoxCommon.ShowDialog("数据恢复完成！", MessageBoxCommon.OK);

        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
